package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    public PageResponse<NursingPlanVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingPlanVo> byPage=nursingPlanMapper.getByPage(name,status);
        return PageResponse.of(byPage, NursingPlanVo.class);
    }

    @Override
    public List<NursingPlanVo> selectAllNursingPlan() {
        List<NursingPlanVo> nursingPlanVo=nursingPlanMapper.selectAllNursingPlan();
        return nursingPlanVo;
    }

    @Override
    public void insertNursingPlan(NursingPlanDto nursingPlanDto) {
        //新增护理计划
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setCreateTime(LocalDateTime.now());
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        //新增护理项目
        if (nursingPlan.getProjectPlans()!=null && nursingPlan.getProjectPlans().size()>0){
            for (NursingProjectPlan projectPlan : nursingPlan.getProjectPlans()) {
                projectPlan.setPlanId(nursingPlan.getId());
                projectPlan.setCreateTime(nursingPlan.getCreateTime());
            }
        }
        nursingProjectPlanMapper.saveBatch(nursingPlan.getProjectPlans());

    }

    @Override
    public NursingPlanVo getById(Long id) {
        NursingPlan nursingPlan=nursingPlanMapper.getById(id);
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        return nursingPlanVo;
    }

    @Override
    public void updateNursingPlan(NursingPlanDto nursingPlanDto) {
        //修改护理计划表
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlanMapper.updateNursingPlan(nursingPlan);
        //先删除修改护理计划项目表
        nursingProjectPlanMapper.deleteNursing(Arrays.asList(Math.toIntExact(nursingPlan.getId())));
        //再全部添加
        if (nursingPlan.getProjectPlans()!=null && nursingPlan.getProjectPlans().size()>0){
            for (NursingProjectPlan projectPlan : nursingPlan.getProjectPlans()) {
                projectPlan.setPlanId(nursingPlan.getId());
                projectPlan.setCreateTime(nursingPlan.getCreateTime());
            }
        }
        nursingProjectPlanMapper.saveBatch(nursingPlan.getProjectPlans());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteNursingPlan(List<Integer> id) {
        //删除护理计划
        nursingPlanMapper.deleteNursingPlan(id);
        //删除护理项目
        nursingProjectPlanMapper.deleteNursing(id);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        nursingPlanMapper.updateStatus(id,status);
    }
}
